﻿/*Người Tạo: mr.Chau
 *Ngày Tạo: 15/05/2013
 *Ghi Chú:
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NangLuongTieuDiemDTO.DTO;
using System.Data.SqlClient;

namespace NangLuongTieuDiemDAO.DAO
{
    public class LoaiSanPhamChinhDAO
    {
        #region "[Insert]"
        public static int Insert(LoaiSanPhamChinhDTO _obj)
        {
            int IDResult = -1;

            try {
                // Mo ket noi
                SqlConnection cn = NangLuongTieuDiemDAO.DataHelper.SqlDataHelper.ConnectionData();

                // Chuoi sql thao tac csdl
                string strSQL = "insert into LoaiSanPhamChinh(TenLoai, Xoa, GhiChu) values('" + _obj.TenLoai + "','" + _obj.Xoa.ToString() + "',N'" + _obj.GhiChu + "')";

                // Thuc thi chuoi sql
                SqlCommand cmd = new SqlCommand(strSQL, cn);

                cmd.ExecuteNonQuery();

                // lay ID cua dong insert

                strSQL = "Select @@IDENTITY";
                cmd = new SqlCommand(strSQL, cn);
                IDResult = Convert.ToInt32(cmd.ExecuteScalar());

                // dong ket noi csdl
                cn.Close();
            }
            catch(Exception ex)
            {
                InSert_LoiHeThong("Insert", ex);
            }
            return IDResult;
        }
        #endregion

        #region "[Delete]"
        public static bool Delete(int _ID)
        {
            bool isSuccess = false;
            try {
                // Mo ket noi
                SqlConnection cn = NangLuongTieuDiemDAO.DataHelper.SqlDataHelper.ConnectionData();

                // Chuoi sql thao tac csdl
                string strSQL = " Delete LoaiSanPhamChinh where ID = " + _ID.ToString();


                // Thuc thi chuoi sql
                SqlCommand cmd = new SqlCommand(strSQL, cn);

                cmd.ExecuteNonQuery();
                
                // dong ket noi csdl
                cn.Close();
                isSuccess = true;
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("Delete", ex);
            }
            return isSuccess;
        }
        #endregion

        #region "[Update]"
        public static bool Update(LoaiSanPhamChinhDTO _obj)
        {
            bool isSuccess = false;
            try {
                // Mo ket noi
                SqlConnection cn = NangLuongTieuDiemDAO.DataHelper.SqlDataHelper.ConnectionData();

                // Chuoi sql thao tac csdl
                string strSQL = "Update LoaiSanPhamChinh Set TenLoai = '" + _obj.TenLoai + "', Xoa = '" + _obj.Xoa.ToString() + "' , GhiChu = N'" + _obj.GhiChu + "' where ID = " + _obj.ID;

                // Thuc thi chuoi sql
                SqlCommand cmd = new SqlCommand(strSQL, cn);

                int I = cmd.ExecuteNonQuery();
                
                // dong ket noi csdl
                cn.Close();
                if (I > 0)
                    isSuccess = true;
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("Update", ex);
            }
            return isSuccess;
        }
        #endregion

        #region "[GetAll]"
        public static List<LoaiSanPhamChinhDTO> GetAll()
        {
            List<LoaiSanPhamChinhDTO> listObjs = new List<LoaiSanPhamChinhDTO>  ();
            try {
              

                // Mo ket noi
                SqlConnection cn = NangLuongTieuDiemDAO.DataHelper.SqlDataHelper.ConnectionData();

                // Chuoi sql thao tac csdl
                string strSQL = " Select * From LoaiSanPhamChinh";


                // Thuc thi chuoi sql
                SqlCommand cmd = new SqlCommand(strSQL, cn);

                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    LoaiSanPhamChinhDTO ItemInsert = new LoaiSanPhamChinhDTO();
                    ItemInsert.ID = int.Parse(dr["ID"].ToString());
                    ItemInsert.TenLoai = dr["TenLoai"].ToString();
                    ItemInsert.Xoa = bool.Parse(dr["Xoa"].ToString());
                    ItemInsert.GhiChu = dr["GhiChu"].ToString();

                    listObjs.Add(ItemInsert);
                }

                cn.Close();
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("GetAll", ex);
            }
            return listObjs;
        }
        #endregion

        #region "[SelectByPrimaryKey]"
        public static LoaiSanPhamChinhDTO SelectByPrimaryKey(int _ID)
        {
            LoaiSanPhamChinhDTO objResult = null;
            try {
                SqlConnection cn;

                // Mo ket noi
                cn = NangLuongTieuDiemDAO.DataHelper.SqlDataHelper.ConnectionData();

                // Chuoi sql thao tac csdl
                string strSQL = " Select * From LoaiSanPhamChinh where ID = "+_ID.ToString();


                // Thuc thi chuoi sql
                SqlCommand cmd = new SqlCommand(strSQL, cn);

               

                SqlDataReader dr = cmd.ExecuteReader();

                objResult = new LoaiSanPhamChinhDTO();


                while (dr.Read())
                {
                    objResult.ID = int.Parse(dr["ID"].ToString());
                    objResult.TenLoai = dr["TenLoai"].ToString();
                    objResult.Xoa = bool.Parse(dr["Xoa"].ToString());
                    objResult.GhiChu = dr["GhiChu"].ToString();

                }

                // Dong ket noi csdl
                cn.Close();
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("SelectByPrimaryKey", ex);
            }
            return objResult;
        }
        #endregion

        #region "[Hàm insert lỗi hệ thống]"
        public static void InSert_LoiHeThong(string TenHam, Exception ex)
        {
            LoiHeThongDTO lhtDto = new LoiHeThongDTO();
            lhtDto.TenLoi = ex.Message.ToString();
            lhtDto.NgayTao = DateTime.Now;
            lhtDto.TenHamBiLoi = "LoaiSanPhamChinhDAO_" + TenHam;
            lhtDto.SuaLoi = false;
            lhtDto.GhiChu = "";
            LoiHeThongDAO.Insert(lhtDto);

        }
        #endregion
    }
}
